/**
 * jims
 */
package com.jims.master.modules.finance.web;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.DateUtils;
import com.jims.master.modules.doctor.entity.ClinicMaster;
import com.jims.master.modules.doctor.service.ClinicMasterService;
import com.jims.master.modules.finance.entity.OutpPaymentsMone;
import com.jims.master.modules.finance.entity.RegistCost;
import com.jims.master.modules.finance.service.RegistCostService;
import com.jims.master.modules.sys.entity.User;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.jims.master.common.data.StringData;
import com.jims.master.common.web.BaseController;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.modules.finance.entity.OutpAcctMaster;
import com.jims.master.modules.finance.service.OutpAcctMasterService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Controller
 * @author chentao
 * @version 2017-03-14
 */
@Controller
@RequestMapping(value = "${adminPath}/finance/outpAcctMaster")
public class OutpAcctMasterController extends BaseController {

	@Autowired
	private OutpAcctMasterService outpAcctMasterService;
	@Autowired
	private ClinicMasterService clinicMasterService;
	@Autowired
	private RegistCostService registCostService;
	@ModelAttribute
	public OutpAcctMaster get(@RequestParam(required=false) String id) {
		OutpAcctMaster entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = outpAcctMasterService.get(id);
		}
		if (entity == null){
			entity = new OutpAcctMaster();
		}
		return entity;
	}

	/**
	 * 结账确认
	 * @param outpAcctMaster
	 * @param startTime
	 * @param endTime
	 * @param acctNo
	 * @return StringData
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(OutpAcctMaster outpAcctMaster, String startTime,String endTime,String acctNo) {
		StringData data=new StringData();
		outpAcctMaster.setAcctNo(acctNo);
		outpAcctMaster.setCreateBy(UserUtils.getUser());
		outpAcctMaster.setStartTime(startTime);
		outpAcctMaster.setEndTime(endTime);
		int i=outpAcctMasterService.save(outpAcctMaster);
		if(i>0){
			data.setCode("success");
			data.setData("保存成功");
		}else{
			data.setCode("error");
			data.setData("保存失败");
		}
		return data;
	}

	/**
	 * 挂号结账
	 * @param model
	 * @param startTime
	 * @param endTime
	 * @return String
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "registAcct")
	public  String registAcct(Model model,String startTime,String endTime){
		User user = UserUtils.getUser();
		user.setType("0");
		String maxAcctNo = outpAcctMasterService.getMaxAcctNo();
        String maxAcctDate = outpAcctMasterService.getMaxAcctDate(user);
		if(maxAcctDate!=null && maxAcctDate!=""){
			startTime =maxAcctDate;
		}
		if(endTime==null || endTime==""){
			endTime=DateUtils.getDate("yyyy-MM-dd HH:mm:ss");
		}
		model.addAttribute("startTime", startTime);
		model.addAttribute("endTime", endTime);
		model.addAttribute("maxAcctNo", maxAcctNo);
		return "/modules/finance/clinicAcct";
	}

	/**
	 * 挂号结账数据查询
	 * @param startTime
	 * @param endTime
	 * @param clinicMaster
	 * @return String
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "registAcctInfo")
	public String  registAcctInfo(String startTime,String endTime,ClinicMaster clinicMaster,Model model){
        clinicMaster.setCheckStartTime(startTime);
		clinicMaster.setCheckEndTime(endTime);
		clinicMaster.setType("1");
		clinicMaster.setCardFlag("0");
		clinicMaster.setCreateBy(UserUtils.getUser());
        ClinicMaster master=clinicMasterService.clinicCheckout(clinicMaster);
        List<RegistCost> registCostList = registCostService.getRegistCosts(clinicMaster);
        List<ClinicMaster> clinicMasters = clinicMasterService.findCharges(clinicMaster);
		model.addAttribute("clinicMaster",master);
		model.addAttribute("registCosts",registCostList);
		model.addAttribute("costs",clinicMasters);
		return "/modules/finance/clinicAcctInfo";
	}

	/**
	 * 挂号结账--确认
	 * @param startTime
	 * @param endTime
	 * @param acctNo
	 * @param clinicMaster
	 * @return StringData
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "clinicAcctCon")
	@ResponseBody
	public StringData clinicAcctCon(String startTime,String endTime,String acctNo,ClinicMaster clinicMaster){
		StringData data=new StringData();
		clinicMaster.setCheckStartTime(startTime);
		clinicMaster.setCheckEndTime(endTime);
		ClinicMaster master=clinicMasterService.clinicCheckout(clinicMaster);
		master.setCheckStartTime(startTime);
		master.setCheckEndTime(endTime);
		List<RegistCost> registCostList = registCostService.getRegistCosts(clinicMaster);
		List<ClinicMaster> clinicMasters = clinicMasterService.findCharges(clinicMaster);
        int i=outpAcctMasterService.saveAcct(master,clinicMasters,acctNo,registCostList);
		if(i>0){
			data.setCode("success");
			data.setData("保存成功");
		}else{
			data.setCode("error");
			data.setData("保存失败");
		}
		return data;
	}

	/**
	 * 合并结账页面加载
	 * @param model
	 * @param endTime
	 * @return
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "mergeAcctPage")
    public String mergeAcctPage(Model model,String endTime,OutpAcctMaster outpAcctMaster){
		if(endTime==null || endTime.equals("")){
			endTime=DateUtils.getDate("yyyy-MM-dd HH:mm:ss");
		}
		outpAcctMaster.setAcctType("1");//非一卡通结账
		outpAcctMaster.setEndTime(endTime);
        outpAcctMaster.setCardFlag("1");//
		outpAcctMaster.setCreateBy(UserUtils.getUser());
        OutpAcctMaster  master=outpAcctMasterService.mergeAcctGetInfo(outpAcctMaster);
 		List<OutpPaymentsMone> paymentsMones = outpAcctMasterService.mergeAcctGetPayInfo(outpAcctMaster);
		model.addAttribute("endTime",endTime);
		model.addAttribute("outpAcctMaster",master);
		model.addAttribute("paymentsMones",paymentsMones);
		return "/modules/finance/mergeAcctIndex";
	}

	/**
	 * 合并结账保存结账信息
	 * @param outpAcctMaster
	 * @return StringData
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "mergeAcctSaveInfo")
	@ResponseBody
    public StringData mergeAcctSaveInfo(OutpAcctMaster outpAcctMaster){
		 StringData data= new StringData();
         int i = outpAcctMasterService.mergeAcctSaveInfo(outpAcctMaster);
		if(i>0){
			data.setCode("success");
			data.setData("合并结账成功");
		}else{
			data.setCode("error");
			data.setData("合并结账失败");
		}
		 return data;
	}
	/**
	 * 门诊结账查询
	 * @param model
	 * @param outpAcctMaster
	 * @param request
	 * @param response
	 * @return String
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "acctSearch")
	public String acctSearch(Model model,OutpAcctMaster outpAcctMaster,HttpServletRequest request,HttpServletResponse response){
		outpAcctMaster.setCreateBy(UserUtils.getUser());
		if(outpAcctMaster.getStartTime()==null  || outpAcctMaster.getStartTime()==""){
			outpAcctMaster.setStartTime(DateUtils.getDate("yyyy-MM-dd HH:mm:ss"));
		}
		if(outpAcctMaster.getEndTime()==null || outpAcctMaster.getEndTime()==""){
			outpAcctMaster.setEndTime(DateUtils.getDate("yyyy-MM-dd HH:mm:ss"));
		}
		Page<OutpAcctMaster> page=outpAcctMasterService.findPage(new Page<OutpAcctMaster>(request,response),outpAcctMaster);
		model.addAttribute("page",page);
		model.addAttribute("outpAcctMaster",outpAcctMaster);
		return "/modules/finance/searchAcctMaster";
	}
	/**
	 * 门诊结账查询 打印
	 * @param outpAcctMaster
	 * @return StringData
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "clinicAcctSearchPrint")
	@ResponseBody
	public StringData clinicAcctSearchPrint(OutpAcctMaster outpAcctMaster){
		StringData stringData = new StringData();
        String printPath=outpAcctMasterService.registAcctPrint(outpAcctMaster);
		stringData.setCode("success");
		stringData.setData(printPath);
		return stringData;
	}
}